package cz.controller;

import com.alibaba.fastjson.JSONObject;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.SftpException;

import cz.HttpGetUtil;
import cz.service.UserService;
import cz.util.net.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

@Controller
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/text")
    public void text(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        String redirect_uri= URLEncoder.encode("http://m.ambtuiguang-0015.cn/user/text1","utf-8");
            String appid = "wxb3618ab148a1747a";
        StringBuffer url=new StringBuffer("https://open.weixin.qq.com/connect/oauth2/authorize?redirect_uri="+redirect_uri+
                "&appid="+appid+"&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
        try {
            System.out.println(url.toString());
            response.sendRedirect(url.toString());
        }catch (Exception e){
            System.out.println(1);
        }
    }
    @PostMapping(value = "/text1")
    @ResponseBody
    public void text1(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{
            text2(request,response);
    }
    @GetMapping(value = "/text2")
    public void text2(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
        response.setContentType("text/html");

        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String code = request.getParameter("code");//获取code
        Map params = new HashMap();
        params.put("secret", "bf2f82246f9df8f0ada7d64e09e23b0c");
        params.put("appid", "wxb3618ab148a1747a");
        params.put("grant_type", "authorization_code");
        params.put("code", code);
        String result = HttpGetUtil.httpRequestToString(
                "https://api.weixin.qq.com/sns/oauth2/access_token", params);
        JSONObject jsonObject = JSONObject.parseObject(result);

        String openid = jsonObject.get("openid").toString();
        System.out.println("得到的openid为:"+openid);
    }
    @RequestMapping(value = "/index")
    public String index(HttpServletRequest request, HttpServletResponse response) {
        String code  = request.getParameter("code");
        //程震测试账号
        //JSONObject jsonObject = HttpUtil.httpsRequest("https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx74f0b4d030cdae94&secret=45c079284355ebf69af58e5eee18cd25&code="+code+"&grant_type=authorization_code","GET", null);
        //韩浩测试账号
//       JSONObject jsonObject = HttpUtil.httpsRequest("https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx0910213a09359dd4&secret=ae54f06542d72ebf1e49b69d7f939271&code="+code+"&grant_type=authorization_code","GET", null);
        //孙吴城管
        JSONObject jsonObject = HttpUtil.httpsRequest("https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx9893470477aed97e&secret=fa230874cf470202dfd5c889c32de06f&code="+code+"&grant_type=authorization_code","GET", null);
        System.out.println(jsonObject);
        JSONObject json = HttpUtil.httpsRequest("https://api.weixin.qq.com/sns/userinfo?access_token="+jsonObject.get("access_token")+"&openid="+jsonObject.get("openid")+"&lang=zh_CN","GET", null);
        System.out.println(json);
        return "user/index";
    }

}
